//
//  myView.m
//  创建动画01
//
//  Created by KL on 17/3/16.
//  Copyright © 2017年 KM. All rights reserved.
//

#import "myView.h"

@implementation myView

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    //初始化路径
    [self drawSubView];

    
//    CGMutablePathRef path = CGPathCreateMutable();
//    CGRect _rect = CGRectMake(60, 60, 200, 280);
//    CGPathAddRect(path, NULL, _rect);
//    
//    //创建画笔绘制路径
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    [[UIColor blueColor]setStroke];
//    [[UIColor grayColor]setFill];
//    CGContextSetLineWidth(context, 5);
//    CGContextAddPath(context, path);
//    CGContextDrawPath(context, kCGPathStroke);
//    
//    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//    animation.duration = 3;
//    animation.timingFunction = [CAMediaTimingFunction functionWithName:@"linear"];
//    animation.repeatCount = 20;
//    animation.path = path;
//    [subView.layer addAnimation:animation forKey:@"subView"];
//    CGContextRelease(context);
    [self drawImage];
}


- (void)drawImage
{
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect rect = CGRectMake(20, 60, 500, 1);
    CGPathAddRect(path, NULL, rect);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor whiteColor]setStroke];
    [[UIColor whiteColor]setFill];
    CGContextSetLineWidth(context, 2);
    CGContextAddPath(context, path);
    CGContextDrawPath(context, kCGPathStroke);
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.duration = 20;//运动时间
    animation.timingFunction = [CAMediaTimingFunction functionWithName:@"linear"];
    animation.repeatCount = 10;//重复次数
    animation.path = path;
    [subView.layer addAnimation:animation forKey:@"subView"];
    CGContextRelease(context);
}
- (void)drawSubView
{
    subView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 30, 30, 30)];
    UIImage *image = [UIImage imageNamed:@"th"];
//    [image drawAtPoint:CGPointMake(10, 10)];
//    [image drawAsPatternInRect:CGRectMake(0, 200, 200, 200)];
    subView.image = image;
    [self addSubview:subView];
}

@end

